package com.roc.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import com.roc.util.Constance;

public class PageCountTag extends TagSupport {
	
	

	
	private static final long serialVersionUID = 2179595893443199007L;
	//public static List<Integer> PAGESIZE=initPageSize();
	private int max_pages;
	private String currentPage;
	private String totalPage;
	private String action;
	
	
	@Override
	public int doStartTag() throws JspException {
		//表示标签所在页面的PageContext
		PageContext  pc=super.pageContext;
		//标签所在页面的out隐含对象
		JspWriter out=pc.getOut();
		try {
			if(!Constance.isNotEmpty(currentPage)){
				currentPage="1";
			}
			if(!Constance.isNotEmpty(totalPage)){
				totalPage="1";
			}
			int cPage=Integer.parseInt(currentPage);
			int tPage=Integer.parseInt(totalPage);
			int outPages=0;
			int middlePage=0;
			
			//System.out.println(cPage+"------"+tPage+"-----"+max_pages);
				out.println(" <span class='statement'>每页<b>"+Constance.USER_PAGE_SIZE+"</b>条记录</span>");
			
			out.println("<span class='total'>共<b>"+cPage+"/"+tPage+"</b>页</span>");
			if(max_pages%2==0){
				middlePage=max_pages/2;
			}else{
				middlePage=(max_pages+1)/2;
			}
			if(tPage<=1){
				out.println("<a href='#' class='nullity'>首 页</a><a href='#' class='nullity'>上一页</a>");

			}else{
				
				if(cPage<=1){
					out.println("<a href='#' class='nullity'>首 页</a>");
					out.println("<a href='#' class='nullity'>上一页</a>");
				}else{
					out.println("<a href='"+action+1+"'>"+"首 页"+"</a>");
					out.println("<a href='"+action+getLastPage(currentPage)+"'>"+"上一页"+"</a>");
				}
			}
			
			if(tPage<=max_pages){
				for(int i=outPages+1;i<=tPage;i++){
					out.println("<a  name='num' href='"+action+i+"'>"+i+"</a>");
					//System.out.println("<a name='num'  href='"+action+i+"'>"+i+"</a>");

				}
			}
			if(tPage>max_pages){
				if(cPage<=middlePage){
					for(int i=outPages+1;i<=max_pages;i++){
						out.println("<a name='num' href='"+action+i+"'>"+i+"</a>");
						//System.out.println("<a name='num' href='"+action+i+"'>"+i+"</a>");

					}
				}
				if(cPage>(tPage-middlePage)){
					outPages=tPage-max_pages;
					for(int i=outPages+1;i<=tPage;i++){
						out.println("<a name='num' href='"+action+i+"'>"+i+"</a>");
						//System.out.println("<a name='num' href='"+action+i+"'>"+i+"</a>");

					}
				}
				if(cPage>middlePage&&cPage<=(tPage-middlePage)){
					outPages=cPage-middlePage;
					for(int i=outPages+1;i<=outPages+max_pages;i++){
						out.println("<a name='num' href='"+action+i+"'>"+i+"</a>");
						//System.out.println("<a name='num'  href='"+action+i+"'>"+i+"</a>");
					}
				}
			}
			if(tPage<=1){
				out.println("<a href='#' class='nullity'>下一页</a><a href='#' class='nullity'>末页</a>");
			}else{
				
				if(cPage>=tPage){
					out.println("<a href='#' class='nullity'>下一页</a><a href='#' class='nullity'>末页</a>");

				}else {
					out.println("<a href='"+action+getNextPage(currentPage,totalPage)+"'>"+"下一页"+"</a>");
					out.println("<a href='"+action+tPage+"'>"+"末页"+"</a>");
					
				}
				
			}
			//System.out.println(out.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			//System.out.println(currentPage+":"+totalPage);
		}
		//表示忽略标签体
		return super.SKIP_BODY;
		//执行标签体
		//return super.EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		
		
		//表示此标签以下的所有代码照常执
		return super.EVAL_PAGE;
		//表示此标签以下的所有代码不执行
		//return super.SKIP_PAGE;
	}

	public void setCurrentPage(String currentPage){
		this.currentPage=currentPage;
	}
	public void setTotalPage(String totalPage){
		this.totalPage=totalPage;
	}
	public void setAction(String action){
		this.action=action;
	}
	public void setMax_pages(int max_pages) {
		this.max_pages = max_pages;
	}

	public static String getLastPage(String currentPage){
		int lastPage=Integer.parseInt(currentPage);
		if(lastPage>1){
			lastPage-=1;
		}else{
			lastPage=1;
		}
		return String.valueOf(lastPage);
	}
	public static String getNextPage(String currentPage,String totalPage){
		int nextPage=Integer.parseInt(currentPage);
		int totalPages=Integer.parseInt(totalPage);
		if(nextPage<totalPages){
			nextPage+=1;
		}else{
			nextPage=totalPages;
		}
		return String.valueOf(nextPage);
	}
	public int getOutPages(String currentPage,String totalPage){
		int cPage=Integer.parseInt(currentPage);
		int tPage=Integer.parseInt(totalPage);
		int outPages=0;
		if(tPage<=10){
			for(int i=outPages+1;i<=tPage;i++){
				System.out.println(i);
			}
		}
		if(tPage>10){
			if(cPage<=5){
				for(int i=outPages+1;i<=max_pages;i++){
					System.out.println(i);
				}
			}
			if(cPage>(tPage-5)){
				outPages=tPage-max_pages;
				for(int i=outPages+1;i<=tPage;i++){
					System.out.println(i);
				}
			}
			if(cPage>5&&cPage<=(tPage-5)){
				outPages=cPage-5;
				for(int i=outPages+1;i<=outPages+max_pages;i++){
					System.out.println(i);
				}
			}
		}
		return 0;
	}
	
	public static List<Integer> initPageSize(){
		List<Integer> pageSize=new ArrayList<Integer>();
		pageSize.set(0,Constance.USER_PAGE_SIZE);
		return pageSize;
	}
	/*public static void main(String [] args){
		//getOutPages("2", "10");
		//getOutPages("2", "13");
		//getOutPages("6", "13");
		//getOutPages("9", "16");
		//getOutPages("11", "13");
	} */
}

